package view.servlet;

import com.alibaba.fastjson.JSONObject;
import entity.Admin;
import entity.Attraction;
import entity.User;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import service.AdminService;
import service.AttractionService;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
    private AdminService adminService;
    private UserService userService;

    @Override
    public void init() throws ServletException {
        super.init();
        adminService = new AdminService();
        userService = new UserService();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methodName = req.getParameter("methodName");
        System.out.println(methodName);
        //通过反射调用不同的方法
        try {
            Method method = this.getClass().getDeclaredMethod(methodName,
                    HttpServletRequest.class, HttpServletResponse.class);
            System.out.println(method);
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        Admin admin = new Admin(null, userName, password);
        System.out.println(admin);
        boolean res = adminService.login(new Admin(null, userName, password));
        PrintWriter out = resp.getWriter();
        if(res){
            req.getSession().setAttribute("login_status", true);
            out.print("登陆成功");
        }else{
            out.print("登陆失败");
        }
    }

    /**
     * 查询所有的用户
     */
    private void getAllUser(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //检查是否已经登录
        Object o = req.getSession().getAttribute("login_status");
        if(o!=null && o.equals(true)){
            String condition = req.getParameter("condition");
            System.out.println(condition);
            PrintWriter pw = resp.getWriter();
            if(condition.equals("all")){
                System.out.println("getAll");
                List<User> users = userService.getAll();
                System.out.println(users);
                String jsonOutput = JSONObject.toJSONString(users);
                System.out.println(jsonOutput);
                pw.print(jsonOutput);
            }
        }
    }

    /**
     * 注销
     */
    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        resp.setCharacterEncoding("UTF-8");
        //使得session立即无效
        req.getSession().invalidate();
        System.out.println("注销登录");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().print("注销成功");
    }

    /**
     * 删除用户
     */
    private void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        req.setCharacterEncoding("UTF-8");
        //检查是否已经登录
        Object o = req.getSession().getAttribute("login_status");
        if(o==null || !o.equals(true)) return;

        //删除这个用户
        String username = req.getParameter("username");
        boolean res = userService.deleteOneUser(username);

        //删除这个用户所有的订单


        //设置发送到客户端响应的内容类型
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        if(res) {
            out.print("true");
        }else{
            out.print("false");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
